//
//  ZLTransitionDelegate.m
//  AnimationDemo
//
//  Created by jiaozenglian on 2020/7/12.
//  Copyright © 2020 Jiaozl. All rights reserved.
//

#import "ZLTransitionDelegate.h"
#import "ZLInteractiveTransition.h"
@interface ZLTransitionDelegate ()

@property (nonatomic, assign)  UINavigationControllerOperation navOperation;

@end

@implementation ZLTransitionDelegate

// MARK: - UIViewControllerTransitioningDelegate
- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source
{
    if (self.animatedPresentTransition) {
        id<UIViewControllerAnimatedTransitioning> aniTransition = self.animatedPresentTransition();
        return aniTransition;
    }
    return nil;
}

- (nullable id <UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed
{
    if (self.animatedDismissalTransition) {
        id<UIViewControllerAnimatedTransitioning> aniTransition = self.animatedDismissalTransition();
        return aniTransition;
    }
    return nil;
}

- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForPresentation:(id <UIViewControllerAnimatedTransitioning>)animator
{
    if (self.interactivePresentTransition ) {
        ZLInteractiveTransition *interactiveTransition = self.interactivePresentTransition();
        return interactiveTransition ? interactiveTransition : nil;
    }
    return nil;
}

- (nullable id <UIViewControllerInteractiveTransitioning>)interactionControllerForDismissal:(id <UIViewControllerAnimatedTransitioning>)animator
{
    if (self.interactiveDismissalTransition ) {
        ZLInteractiveTransition *interactiveTransition = self.interactiveDismissalTransition();
        return interactiveTransition ? interactiveTransition : nil;
    }
    return nil;
}

- (nullable UIPresentationController *)presentationControllerForPresentedViewController:(UIViewController *)presented presentingViewController:(nullable UIViewController *)presenting sourceViewController:(UIViewController *)source API_AVAILABLE(ios(8.0))
{
    if (self.presentationControllerForTransition) {
        UIPresentationController *presentationC = self.presentationControllerForTransition();
        NSAssert(presentationC.presentedViewController == presented, @"You didn't initialize %@ with the correct presentedViewController.  Expected %@, got %@.",
                     presentationC, presented, presentationC.presentedViewController);
        return presentationC;
    }
    return [[UIPresentationController alloc] initWithPresentedViewController:presented presentingViewController:source];
}


// MARK: - UINavigationControllerDelegate
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC
{
    self.navOperation = operation;
    //分pop和push两种情况分别返回动画过渡代理相应不同的动画操作
    if(operation == UINavigationControllerOperationPush ) {
        if (self.animatedPresentTransition) {
            id<UIViewControllerAnimatedTransitioning> aniTransition = self.animatedPresentTransition();
            return aniTransition;
        }
    } else if (operation == UINavigationControllerOperationPop ) {
        if (self.animatedDismissalTransition) {
            id<UIViewControllerAnimatedTransitioning> aniTransition = self.animatedDismissalTransition();
            return aniTransition;
        }
    }
    return nil;
}

- (id<UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController interactionControllerForAnimationController:(id<UIViewControllerAnimatedTransitioning>)animationController
{
    if (self.navOperation == UINavigationControllerOperationPush ) {
        if (self.interactivePresentTransition ) {
            ZLInteractiveTransition *interactiveTransition = self.interactivePresentTransition();
            return interactiveTransition ? interactiveTransition : nil;
        }
    } else if (self.navOperation == UINavigationControllerOperationPop ) {
        if (self.interactiveDismissalTransition ) {
            ZLInteractiveTransition *interactiveTransition = self.interactiveDismissalTransition();
            return interactiveTransition ? interactiveTransition : nil;
        }
    }
    return nil;
}

- (void)dealloc
{
    NSLog(@"%s", __func__);
}

@end
